/*
 * Filename:    AuditDispatchController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.ICaseCheckService;
import com.paic.mhis.hcpms.audit.dto.AuditCaseReportDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserRequestDTO;
import com.paic.mhis.hcpms.audit.dto.AuditDispatchUserResponseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditNotificationDTO;
import com.paic.mhis.hcpms.audit.dto.AuditReportRespDTO;
import com.paic.mhis.hcpms.audit.dto.BatchSubmitResponseDTO;
import com.paic.mhis.hcpms.audit.dto.CaseCheckConfirmRequestDTO;
import com.paic.mhis.hcpms.audit.dto.CaseCheckResponseDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 案件审核的controller
 * 
 * @author EX-WANGKUI001
 * 
 */
@Controller
@RequestMapping("/caseCheckAudit/")
public class CaseCheckController {
	@BizServicesProxy(beanName = "caseCheckServcieImpl")
	private ICaseCheckService caseCheckService;

	/**
	 * 稽核审核的批量操作逻辑处理
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("caseBatchSubmit.do")
	@ResponseBody
	public BatchSubmitResponseDTO caseBatchSubmit(HttpServletRequest request)
			throws Exception {

		BatchSubmitResponseDTO messageBean = new BatchSubmitResponseDTO();

		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String[] listCodes = request.getParameterValues("caseCodes");
		String opinion = request.getParameter("opinion");
		String opinionDesc = request.getParameter("opinionDesc");
		try {
			messageBean = this.caseCheckService.processBatchSubmitInfo(
					listCodes, user, opinion, opinionDesc);
		} catch (Exception e) {
			messageBean.setFlag(false);
			messageBean.setMessage("操作失败!");
		}

		return messageBean;
	}

	/**
	 * 审核岗 撤销操作的逻辑处理
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("auditCancelProcess.do")
	@ResponseBody
	public AuditDispatchUserResponseDTO auditCancelProcess(
			HttpServletRequest request) throws Exception {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String caseCode = request.getParameter("caseCode");

		AuditDispatchUserResponseDTO responseBean = new AuditDispatchUserResponseDTO();

		try {
			this.caseCheckService.auditCancelProcess(caseCode, user);
			responseBean.setFlag(true);
			responseBean.setMesssage("操作成功了");
		} catch (Exception e) {
			responseBean.setFlag(false);
			responseBean.setMesssage(e.getMessage());
		}

		return responseBean;
	}

	/**
	 * 更新稽核告知函信息
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("updateAuditTellNotificationInfo.do")
	@ResponseBody
	public void updateAuditTellNotificationInfo(@ModelAttribute
	AuditNotificationDTO vo) {
		this.caseCheckService.updateAuditReportInfoAndNotification(null, vo);
	}

	/**
	 * 更新稽核报告单信息
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("updateAuditReportInfo.do")
	@ResponseBody
	public void updateAuditReportInfo(@ModelAttribute
	AuditCaseReportDTO vo) {

		this.caseCheckService.updateAuditReportInfoAndNotification(vo, null);

	}

	/**
	 * 根据案件编码加载稽核告知函和稽核报告的信息
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("loadAuditNotifiance.do")
	@ResponseBody
	public AuditReportRespDTO loadAuditNotifiance(HttpServletRequest request) {
		String caseCode = request.getParameter("caseCode");
		AuditReportRespDTO returnBean = this.caseCheckService
				.loadReportInfo(caseCode);
		return returnBean;

	}

	/**
	 * 初始化审核意见
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "processTempSave.do", method = RequestMethod.POST)
	@ResponseBody
	public AuditDispatchUserResponseDTO processTempSave(
			HttpServletRequest request) throws Exception {
		AuditDispatchUserResponseDTO responseBean = new AuditDispatchUserResponseDTO();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditDispatchUserRequestDTO vo = JSONObject.toJavaObject(
					transferJson, AuditDispatchUserRequestDTO.class);
			String type = vo.getType();
			vo.setOpertionUsername(user.getUsername());
			responseBean = this.caseCheckService.processTempSave(vo, type,
					transferJson, user.getUsername());
			responseBean.setFlag(true);
		} catch (Exception e) {
			responseBean.setFlag(false);
		}
		return responseBean;
	}

	/**
	 * 根据事件编码和案件号加载对应的事件处理，调查结果，稽核项目，稽核结果
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("loadCaseInfo.do")
	@ResponseBody
	public CaseCheckResponseDTO loadCaseInfo(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String caseCode = request.getParameter("caseCode");
		String problemNo = request.getParameter("problemCode");
		CaseCheckResponseDTO returnBean = this.caseCheckService
				.loadCaseCheckResponseDTOByCaseCode(caseCode, problemNo,
						user.getUsername());
		return returnBean;

	}

	/**
	 * 处理审核岗位对于稽核项目的审核逻辑处理
	 * 
	 * @param vo
	 * @param request
	 */
	@RequestMapping(value = "processCaseCheckAuditConfirm.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> processCaseCheckAuditConfirm(
			HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			CaseCheckConfirmRequestDTO vo = JSONObject.toJavaObject(
					transferJson, CaseCheckConfirmRequestDTO.class);

			String username = user.getUsername();
			vo.setOpertionUsername(username);

			this.caseCheckService.processCaseCheckAuditConfirm(vo, user,
					transferJson);
			result.put("result", "success");
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
}
